/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: DuKe TeAm
 * License Type: Purchased
 */
package orm.impl;

import org.orm.*;
import org.hibernate.Query;
import java.util.List;
import orm.*;

public class RegionDAOImpl implements orm.dao.RegionDAO {
	private static final org.apache.log4j.Logger _logger = org.apache.log4j.Logger.getLogger(RegionDAOImpl.class);
	public Region loadRegionByORMID(String re_numeroregion) throws PersistentException {
		try {
			PersistentSession session = orm.EvaluacionDocentePersistentManager.instance().getSession();
			return loadRegionByORMID(session, re_numeroregion);
		}
		catch (Exception e) {
			_logger.error("loadRegionByORMID(String re_numeroregion)", e);
			throw new PersistentException(e);
		}
	}
	
	public Region getRegionByORMID(String re_numeroregion) throws PersistentException {
		try {
			PersistentSession session = orm.EvaluacionDocentePersistentManager.instance().getSession();
			return getRegionByORMID(session, re_numeroregion);
		}
		catch (Exception e) {
			_logger.error("getRegionByORMID(String re_numeroregion)", e);
			throw new PersistentException(e);
		}
	}
	
	public Region loadRegionByORMID(String re_numeroregion, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.EvaluacionDocentePersistentManager.instance().getSession();
			return loadRegionByORMID(session, re_numeroregion, lockMode);
		}
		catch (Exception e) {
			_logger.error("loadRegionByORMID(String re_numeroregion, org.hibernate.LockMode lockMode)", e);
			throw new PersistentException(e);
		}
	}
	
	public Region getRegionByORMID(String re_numeroregion, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.EvaluacionDocentePersistentManager.instance().getSession();
			return getRegionByORMID(session, re_numeroregion, lockMode);
		}
		catch (Exception e) {
			_logger.error("getRegionByORMID(String re_numeroregion, org.hibernate.LockMode lockMode)", e);
			throw new PersistentException(e);
		}
	}
	
	public Region loadRegionByORMID(PersistentSession session, String re_numeroregion) throws PersistentException {
		try {
			return (Region) session.load(orm.Region.class, re_numeroregion);
		}
		catch (Exception e) {
			_logger.error("loadRegionByORMID(PersistentSession session, String re_numeroregion)", e);
			throw new PersistentException(e);
		}
	}
	
	public Region getRegionByORMID(PersistentSession session, String re_numeroregion) throws PersistentException {
		try {
			return (Region) session.get(orm.Region.class, re_numeroregion);
		}
		catch (Exception e) {
			_logger.error("getRegionByORMID(PersistentSession session, String re_numeroregion)", e);
			throw new PersistentException(e);
		}
	}
	
	public Region loadRegionByORMID(PersistentSession session, String re_numeroregion, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Region) session.load(orm.Region.class, re_numeroregion, lockMode);
		}
		catch (Exception e) {
			_logger.error("loadRegionByORMID(PersistentSession session, String re_numeroregion, org.hibernate.LockMode lockMode)", e);
			throw new PersistentException(e);
		}
	}
	
	public Region getRegionByORMID(PersistentSession session, String re_numeroregion, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Region) session.get(orm.Region.class, re_numeroregion, lockMode);
		}
		catch (Exception e) {
			_logger.error("getRegionByORMID(PersistentSession session, String re_numeroregion, org.hibernate.LockMode lockMode)", e);
			throw new PersistentException(e);
		}
	}
	
	public Region[] listRegionByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.EvaluacionDocentePersistentManager.instance().getSession();
			return listRegionByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			_logger.error("listRegionByQuery(String condition, String orderBy)", e);
			throw new PersistentException(e);
		}
	}
	
	public Region[] listRegionByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.EvaluacionDocentePersistentManager.instance().getSession();
			return listRegionByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			_logger.error("listRegionByQuery(String condition, String orderBy)", e);
			throw new PersistentException(e);
		}
	}
	
	public Region[] listRegionByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Region as Region");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			List list = query.list();
			return (Region[]) list.toArray(new Region[list.size()]);
		}
		catch (Exception e) {
			_logger.error("listRegionByQuery(PersistentSession session, String condition, String orderBy)", e);
			throw new PersistentException(e);
		}
	}
	
	public Region[] listRegionByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Region as Region");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			List list = query.list();
			return (Region[]) list.toArray(new Region[list.size()]);
		}
		catch (Exception e) {
			_logger.error("listRegionByQuery(PersistentSession session, String condition, String orderBy)", e);
			throw new PersistentException(e);
		}
	}
	
	public Region loadRegionByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.EvaluacionDocentePersistentManager.instance().getSession();
			return loadRegionByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			_logger.error("loadRegionByQuery(String condition, String orderBy)", e);
			throw new PersistentException(e);
		}
	}
	
	public Region loadRegionByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.EvaluacionDocentePersistentManager.instance().getSession();
			return loadRegionByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			_logger.error("loadRegionByQuery(String condition, String orderBy)", e);
			throw new PersistentException(e);
		}
	}
	
	public Region loadRegionByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		Region[] regions = listRegionByQuery(session, condition, orderBy);
		if (regions != null && regions.length > 0)
			return regions[0];
		else
			return null;
	}
	
	public Region loadRegionByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		Region[] regions = listRegionByQuery(session, condition, orderBy, lockMode);
		if (regions != null && regions.length > 0)
			return regions[0];
		else
			return null;
	}
	
	public static java.util.Iterator iterateRegionByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.EvaluacionDocentePersistentManager.instance().getSession();
			return iterateRegionByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			_logger.error("iterateRegionByQuery(String condition, String orderBy)", e);
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateRegionByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.EvaluacionDocentePersistentManager.instance().getSession();
			return iterateRegionByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			_logger.error("iterateRegionByQuery(String condition, String orderBy)", e);
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateRegionByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Region as Region");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			return query.iterate();
		}
		catch (Exception e) {
			_logger.error("iterateRegionByQuery(PersistentSession session, String condition, String orderBy)", e);
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateRegionByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Region as Region");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			return query.iterate();
		}
		catch (Exception e) {
			_logger.error("iterateRegionByQuery(PersistentSession session, String condition, String orderBy)", e);
			throw new PersistentException(e);
		}
	}
	
	public Region createRegion() {
		return new orm.Region();
	}
	
	public boolean save(orm.Region region) throws PersistentException {
		try {
			orm.EvaluacionDocentePersistentManager.instance().saveObject(region);
			return true;
		}
		catch (Exception e) {
			_logger.error("save(orm.Region region)", e);
			throw new PersistentException(e);
		}
	}
	
	public boolean delete(orm.Region region) throws PersistentException {
		try {
			orm.EvaluacionDocentePersistentManager.instance().deleteObject(region);
			return true;
		}
		catch (Exception e) {
			_logger.error("delete(orm.Region region)", e);
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Region region)throws PersistentException {
		try {
			orm.Evaluacion[] lEvaluacions = region.evaluacion.toArray();
			for(int i = 0; i < lEvaluacions.length; i++) {
				lEvaluacions[i].setRegionre_numeroregion(null);
			}
			orm.Comuna[] lComunas = region.comuna.toArray();
			for(int i = 0; i < lComunas.length; i++) {
				lComunas[i].setRegionre_numeroregion(null);
			}
			return delete(region);
		}
		catch(Exception e) {
			_logger.error("deleteAndDissociate()", e);
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Region region, org.orm.PersistentSession session)throws PersistentException {
		try {
			orm.Evaluacion[] lEvaluacions = region.evaluacion.toArray();
			for(int i = 0; i < lEvaluacions.length; i++) {
				lEvaluacions[i].setRegionre_numeroregion(null);
			}
			orm.Comuna[] lComunas = region.comuna.toArray();
			for(int i = 0; i < lComunas.length; i++) {
				lComunas[i].setRegionre_numeroregion(null);
			}
			try {
				session.delete(region);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		catch(Exception e) {
			_logger.error("deleteAndDissociate()", e);
			throw new PersistentException(e);
		}
	}
	
	public boolean refresh(orm.Region region) throws PersistentException {
		try {
			orm.EvaluacionDocentePersistentManager.instance().getSession().refresh(region);
			return true;
		}
		catch (Exception e) {
			_logger.error("refresh(orm.Region region)", e);
			throw new PersistentException(e);
		}
	}
	
	public boolean evict(orm.Region region) throws PersistentException {
		try {
			orm.EvaluacionDocentePersistentManager.instance().getSession().evict(region);
			return true;
		}
		catch (Exception e) {
			_logger.error("evict(orm.Region region)", e);
			throw new PersistentException(e);
		}
	}
	
	public Region loadRegionByCriteria(RegionCriteria regionCriteria) {
		Region[] regions = listRegionByCriteria(regionCriteria);
		if(regions == null || regions.length == 0) {
			return null;
		}
		return regions[0];
	}
	
	public Region[] listRegionByCriteria(RegionCriteria regionCriteria) {
		return regionCriteria.listRegion();
	}
}
